package com.example.administrator.liveapp;

import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;

import com.alivc.player.AccessKey;
import com.alivc.player.AccessKeyCallback;
import com.alivc.player.AliVcMediaPlayer;
import com.example.administrator.liveapp.bean.LoginBean;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;

import org.xutils.BuildConfig;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/22 0022.
 */

public class ApplicationUtils extends Application {
    public String businessId = "chenchenzhibo"; //随便写
    public String accessKeyId ="LTAIy6FuPPH3E0Zk";//是在阿里平台上申请的accessKeyId
    public String accessKeySecret = "UMG9psf1hS5BwCNMzDdsO9r7iCAnBw";//是在阿里平台上申请的accessKeySecret 两个是配套出现的
    public LoginBean loginBean;
    public void onCreate() {
        super.onCreate();
        init();
        instance=this;
        initImageLoader();
        x.Ext.init(this);
        x.Ext.setDebug(true); // 是否输出debug日志, 开启debug会影响性能.
        Config.DEBUG = true;
        UMShareAPI.get(this);
    }

    public List<Activity> activities = new ArrayList<Activity>();

    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();

        for (Activity activity : activities) {
            activity.finish();
        }
        System.exit(0);
    }

    public static ApplicationUtils instance;

    public static ApplicationUtils getInstance() {
        return instance;
    }

    /**
     * 配置ImageLoader
     */
    public static void initImageLoader() {


        // 显示相关的配置
        DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
                // 配置Url加载错误的图片

                //  .showImageForEmptyUri(R.mipmap.ic_launcher).
                //          showImageOnFail(R.mipmap.ic_launcher)
                // .showImageOnLoading(R.mipmap.ic_launcher)
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中
                .considerExifParams(true)
                .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
                //    .displayer(new RoundedBitmapDisplayer(20))
                .build();

        // 性能，内存配置
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(instance).threadPoolSize(3) // 配置2条线程
                .threadPriority(Thread.NORM_PRIORITY - 2) // 配置线程优先级，比UI线程低，这样更流畅点
                .memoryCacheSize(10 * 1024 * 1024) // 配置内存缓存，值为50M
                .defaultDisplayImageOptions(imageOptions) // 默认的显示相关配置。使用的时候也可以在display中使用其他的option来配置
                .threadPoolSize(3).threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(10 * 1024 * 1024))
                .discCacheSize(50 * 1024 * 1024)
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .discCacheFileCount(100)
                .imageDownloader(new BaseImageDownloader(ApplicationUtils.getInstance(), 5 * 1000, 30 * 1000))
                .writeDebugLogs() // Remove for release app
                .build();

        ImageLoader.getInstance().init(configuration);

    }

    public void init() {
        AliVcMediaPlayer.init(getApplicationContext(), businessId, new AccessKeyCallback() {
            public AccessKey getAccessToken() {
                return new AccessKey(accessKeyId, accessKeySecret);
            }
        });
    }
    //各个平台的配置
    {
        PlatformConfig.setWeixin("wxfbcebe26b18a4bcc","e63b1a7b7e847cb0a8ff7ef323e00846");
        PlatformConfig.setQQZone("101414802","ec53c161564f2b490a33b70954a4690d");
        PlatformConfig.setSinaWeibo("11111","","");
    }

    {
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        config.isOpenShareEditActivity(true);
        config.setSinaAuthType(UMShareConfig.AUTH_TYPE_SSO);
        config.setFacebookAuthType(UMShareConfig.AUTH_TYPE_SSO);
        config.setShareToLinkedInFriendScope(UMShareConfig.LINKED_IN_FRIEND_SCOPE_ANYONE);
    }

}
